package com.aliexcel;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

public class Demo1 {

    public static void main(String[] args) {

        try {
            FileOutputStream os = new FileOutputStream(new File("d:/user.xlsx"));

            List<User> lst = new ArrayList<>();
            User user = new User();
            user.setName("yejing");
            user.setAddr("广州");
            user.setAge(15);
            lst.add(user);

            user = new User();
            user.setName("yejing");
            user.setAddr("广州");
            user.setAge(15);
            lst.add(user);

            user = new User();
            user.setName("yejing");
            user.setAddr("广州");
            user.setAge(15);
            lst.add(user);

            user = new User();
            user.setName("yejing");
            user.setAddr("广州");
            user.setAge(15);
            lst.add(user);

            user = new User();
            user.setName("yejing");
            user.setAddr("广州");
            user.setAge(15);
            lst.add(user);


            exportExcel(lst,"用户表",User.class,os);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    public static <T> void exportExcel(List<T> list, String sheetName, Class<T> clazz,OutputStream os) {
        ExcelWriterSheetBuilder builder = EasyExcel.write(os, clazz)
                .autoCloseStream(true)
                .sheet(sheetName);
        // 添加下拉框操作
        builder.doWrite(list);
    }
}
